home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 13 threading / threadingdemo / classes.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  5.3 KB  |  184 lines

  1. Imports System.Threading
  2.  
  3. ' a class that lets you share data with another thread
  4.  
  5. Class ThreadData
  6.     Public Id As Integer
  7.     Public Msg As String
  8.     Public Done As Boolean
  9.  
  10.     ' The entry point for the thread
  11.     Sub DoTheTask()
  12.         Dim i As Integer
  13.         For i = 1 To 10
  14.             ' just show that the thread got the correct values
  15.             Console.WriteLine("{0} (Thread ID = {1})", Msg, Id)
  16.             ' Wait for .2 seconds.
  17.             Thread.CurrentThread.Sleep(200)
  18.         Next
  19.         ' Signal that this thread has completed.
  20.         Done = True
  21.     End Sub
  22. End Class
  23.  
  24. ' a class that lets you share data with another thread
  25. ' and that raises an event when the tasks has completed
  26.  
  27. Class ThreadData2
  28.     Event TaskCompleted(ByVal sender As Object, ByVal endTime As Date)
  29.  
  30.     Public Id As Integer
  31.     Public Msg As String
  32.     Public Done As Boolean
  33.  
  34.     ' The entry point for the thread
  35.     Sub DoTheTask()
  36.         Dim i As Integer
  37.         For i = 1 To 10
  38.             ' just show that the thread got the correct values
  39.             Console.WriteLine("{0} (Thread ID = {1})", Msg, Id)
  40.             ' Wait for .2 seconds.
  41.             Thread.CurrentThread.Sleep(200)
  42.         Next
  43.         ' Signal that this thread has completed.
  44.         Done = True
  45.  
  46.         ' raise an event 
  47.         RaiseEvent TaskCompleted(Me, Date.Now)
  48.     End Sub
  49. End Class
  50.  
  51. ' this class is a base class for a thread wrapper
  52.  
  53. MustInherit Class ThreadWrapperBase
  54.     ' This public member exposes the Thread object.
  55.     Public ReadOnly Thread As System.Threading.Thread
  56.  
  57.     ' The constructor creates the thread object and runs the code
  58.     Sub New()
  59.         Me.Thread = New System.Threading.Thread(AddressOf Me.RunThread)
  60.     End Sub
  61.  
  62.     ' This starts the thread
  63.     Overridable Sub Start()
  64.         Me.Thread.Start()
  65.     End Sub
  66.  
  67.     ' This private procedure is where the thread starts its execution;
  68.     ' when the thread terminates, the Done flag is set to 
  69.     Private Sub RunThread()
  70.         m_Done = False
  71.         OnStart()
  72.         m_Done = True
  73.     End Sub
  74.  
  75.     Dim m_Done As Boolean
  76.  
  77.     ' This property returns True if the thread has completed its task.
  78.     ReadOnly Property Done() As Boolean
  79.         Get
  80.             Return m_Done
  81.         End Get
  82.     End Property
  83.  
  84.     ' Derived classes must override this to provide the actual code for the thread.
  85.     Protected MustOverride Sub OnStart()
  86. End Class
  87.  
  88. ' an example of a thread wrapper
  89.  
  90. Class MyTask
  91.     Inherits ThreadWrapperBase
  92.  
  93.     ' Instance data
  94.     Public Id As Integer
  95.     Public Msg As String
  96.  
  97.     Sub New(ByVal id As Integer, ByVal msg As String)
  98.         Me.Id = id
  99.         Me.Msg = msg
  100.     End Sub
  101.  
  102.     ' The code that the thread must execute
  103.     Protected Overrides Sub OnStart()
  104.         Dim i As Integer
  105.         For i = 1 To 10
  106.             ' just show that the thread got the correct values.
  107.             Console.WriteLine("{0} (Thread ID = {1})", Msg, Id)
  108.             ' Wait for .2 seconds.
  109.             Thread.CurrentThread.Sleep(200)
  110.         Next
  111.     End Sub
  112.  
  113. End Class
  114.  
  115. ' this class is used to demonstrate the ThreadPool
  116.  
  117. Class LightweightTask
  118.     Public SomeData As String
  119.  
  120.     ' The method that contains the interesting code
  121.     ' (Not really interesting in this example).    
  122.     Sub DoTheTask(ByVal state As Object)
  123.         Console.WriteLine("Message from thread #" & state.ToString)
  124.     End Sub
  125. End Class
  126.  
  127. ' this class demonstrates the Synchronization attribute
  128.  
  129. <System.Runtime.Remoting.Contexts.Synchronization()> _
  130. Class Display
  131.     ' Synchronized classes must inherit from ContextBoundObject.
  132.     Inherits ContextBoundObject
  133.  
  134.     Sub DisplayData()
  135.         Console.Write("Message ")
  136.         Console.Write("from ")
  137.         Console.WriteLine(Thread.CurrentThread.Name)
  138.     End Sub
  139. End Class
  140.  
  141. ' this class is used to test the AutoResetEvent class
  142.  
  143. Class FileFinder
  144.     Dim StartPath As String       ' The starting search path
  145.     Dim SearchedPattern As String ' The search pattern (can contain wildcards)
  146.  
  147.     Sub New(ByVal path As String, ByVal search As String)
  148.         Me.StartPath = path
  149.         Me.SearchedPattern = search
  150.     End Sub
  151.  
  152.     ' This is the method with which thread execution starts.
  153.     Sub StartSearch()
  154.         Search(Me.StartPath)
  155.         ' Decrease the number of running threads before exiting.
  156.         Interlocked.Decrement(searchingThreads)
  157.         are.Set()
  158.     End Sub
  159.  
  160.     ' This recursive procedure does the actual job.
  161.     Sub Search(ByVal path As String)
  162.         Dim files() As String
  163.         ' Get all the files that match the search pattern.
  164.         files = System.IO.Directory.GetFiles(path, SearchedPattern)
  165.         ' If there is at least one file, let the main thread know about it.
  166.         If Not files Is Nothing Then
  167.             ' Get a lock on the result ArrayList.
  168.             SyncLock filesAl.SyncRoot
  169.                 ' Add all found files.
  170.                 filesAl.AddRange(files)
  171.                 ' Let the consumer thread know about the new filenames.
  172.                 are.Set()
  173.             End SyncLock
  174.         End If
  175.  
  176.         ' Repeat the search on all subdirectories.
  177.         Dim dirname As String
  178.         For Each dirname In System.IO.Directory.GetDirectories(path)
  179.             Search(dirname)
  180.         Next
  181.     End Sub
  182. End Class
  183.  
  184.